package com.krystal.mapper;

/**
 * @author Krystal1
 * @date 2021/4/26 -14:24
 */

import com.krystal.bean.CovidBean;
import com.krystal.bean.CovidChinaBean;
import com.krystal.bean.CovidChinaDayBean;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;


@Mapper
@Repository
public interface DataMapper {
    /**
     * 清空表
     */
    @Update("truncate table ${tableName}")
    void truncateTable(String tableName);

    /**
     * 初始化更新covid19_0数据库（腾讯新闻）
     */
    @Insert("insert into covid19_0(date,nowSevere,importedCase,localConfirm,dead,nowConfirm,heal,confirm,suspect,deadRate,healRate,noInfect)" +
            "values(#{date},#{nowSevere},#{importedCase},#{localConfirm},#{dead},#{nowConfirm},#{heal},#{confirm},#{suspect},#{deadRate},#{healRate},#{noInfect})")
    void insertInitChinaDay(CovidChinaDayBean covidChinaDayBean);

    /**
     * 查询数据库表covid19_0的数据量
     */
    @Select("select count(*) from covid19_0")
    int selectDataChinaDay();

    /**更新表一*/
    @Insert("INSERT INTO `bigdata`.`covid19_1` (`datetime`, `currentConfirmedCount`, `confirmedCount`, `suspectedCount`" +
            ", `curedCount`, `deadCount`,deadIncr,confirmedIncr,curedIncr,suspectedCountIncr) " +
            "VALUES (#{dateId}, #{currentConfirmedCount}, #{confirmedCount}, #{suspectedCount}, #{curedCount}, " +
            "#{deadCount},#{deadIncr},#{confirmedIncr},#{curedIncr},#{suspectedCountIncr});")
    void insertInitCountry(CovidChinaBean covidChinaBean);

    @Select("select count(*) from covid19_1")
    int selectDataCountry();

    //这一步是初始化数据库省份信息表 后续不会再调用 更新数据使用下面的update方法
    @Insert("insert into covid19_2(datetime,locationId,provinceShortName,cityName,currentConfirmedCount,confirmedCount,suspectedCount,curedCount,deadCount,pid) " +
            "values(#{datetime},#{locationId},#{provinceShortName},#{cityName},#{currentConfirmedCount}," +
            "#{confirmedCount},#{suspectedCount},#{curedCount},#{deadCount},#{pid})")
    void insertInit(CovidBean pCovidBean);


    //以省份短名 来一次更新省份数据的内容
    @Update("update covid19_2 set datetime=#{datetime},locationId=#{locationId},provinceShortName=#{provinceShortName},cityName=#{cityName}," +
            "currentConfirmedCount=#{currentConfirmedCount},confirmedCount=#{confirmedCount},suspectedCount=#{suspectedCount}," +
            "curedCount=#{curedCount},deadCount=#{deadCount},pid=#{pid} where provinceShortName=#{provinceShortName} ")
    void updateDataProvince(CovidBean pcovidBean);

    //这一步是初始化数据库城市信息表 调用一次后不会再使用  更新数据使用下面的update方法
    @Insert("insert into covid19_3(datetime,provinceName,provinceShortName,cityName,currentConfirmedCount,confirmedCount," +
            "suspectedCount,curedCount,deadCount,locationId,pid,statisticsData,cities) " +
            "VALUES(#{datetime},#{provinceName},#{provinceShortName},#{cityName},#{currentConfirmedCount},#{confirmedCount}," +
            "#{suspectedCount},#{curedCount},#{deadCount},#{locationId},#{pid},#{statisticsData},#{cities});")
    void insertInitCity(CovidBean covidBean);

    //以省份短名和cityName两个字段来 更新数据的类容
    @Update("update covid19_3 set datetime=#{datetime},locationId=#{locationId},provinceShortName=#{provinceShortName},cityName=#{cityName}," +
            "currentConfirmedCount=#{currentConfirmedCount},confirmedCount=#{confirmedCount},suspectedCount=#{suspectedCount}," +
            "curedCount=#{curedCount},deadCount=#{deadCount},pid=#{pid} where provinceShortName=#{provinceShortName} and cityName=#{cityName}")
    void updateDataCity(CovidBean covidBean);

    /**
     * 创建第四张表
     **/
    @Insert("insert into covid19_4(datetime,provinceShortName,pid,confirmedCount) values(#{datetime},#{provinceShortName},#{pid},#{confirmedCount})")
    void insertInitImport(CovidBean covidBean);

    /**
     * 查询数据库表covid19_4的最后一条数据的时间，来判断当前数据是否需要更新
     */
    @Select("select datetime from covid19_4 order by id DESC limit 1")
    String selectDateTime();

    //更新数据库表covid19_4的数据
    @Update("update covid19_4 set confirmedCount=#{confirmedCount} where provinceShortName=#{provinceShortName}")
    void updateDataImport(CovidBean covidBean);
}
